package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.MessageService;
import service.impl.MessageServiceImpl;

import entity.Message;

public class MessageSendServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("uid") == null){
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		}
		else{
			String receiveUname = request.getParameter("receiveUname");
			getServletContext().setAttribute("receiveUname", receiveUname);
			request.getRequestDispatcher("./messageSend.jsp").forward(request, response); 
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		if(request.getSession().getAttribute("uid") == null){
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		}
		request.setCharacterEncoding("UTF-8");
		String note = request.getParameter("note");
		String receiveUname = request.getParameter("receiveUname");
		String sendUname = (String) request.getSession().getAttribute("uname");
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		
		Message msg = new Message();
		msg.setNote(note);
		msg.setPostTime(date);
		msg.setReadSign(0);
		msg.setReceiveUname(receiveUname);
		msg.setSendUname(sendUname);
		
		MessageService ms = new MessageServiceImpl();
		ms.sendMessage(msg);
		
		response.sendRedirect(request.getContextPath() + "/index.jsp");
	}

}
